package edu.uhcl.csci4838.group3.test;
import java.util.ArrayList;

import edu.uhcl.csci4838.group3.R;
import edu.uhcl.csci4838.group3.dto.Course;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class CoursesExpandableListAdapter extends BaseExpandableListAdapter {

    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }

    private Context context;

    private ArrayList<String> groups; 

    private ArrayList<ArrayList<Course>> children;

    public CoursesExpandableListAdapter(Context context, ArrayList<String> groups,
            ArrayList<ArrayList<Course>> children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
    }

    /**
     * A general add method, that allows you to add a Vehicle to this list
     * 
     * Depending on if the category opf the vehicle is present or not,
     * the corresponding item will either be added to an existing group if it 
     * exists, else the group will be created and then the item will be added
     * @param vehicle
     */
    public void addItem(Course course) {
        if (!this.groups.contains(course.getSemester().getName())) {
            this.groups.add(course.getSemester().getName());
        }
        int index = this.groups.indexOf(course.getSemester().getName());
        if (this.children.size() < index + 1) {
            this.children.add(new ArrayList<Course>());
        }
        this.children.get(index).add(course);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    // Return a child view. You can load your custom layout here.
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Course course = (Course) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tv_item);
        tv.setText("   " + course.getName());

        
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.category, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tv_category);
        tv.setText(group);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}
